package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.MyListViewAdapter;
import com.lanou3g.musicenjoy.entity.Mp3Info;

import com.lanou3g.musicenjoy.entity.PlayMusicEntity;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBean;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.lanou3g.musicenjoy.utils.CharacterParser;

import com.lanou3g.musicenjoy.utils.FindSongs;
import com.lanou3g.musicenjoy.utils.PinyinComparator;
import com.lanou3g.musicenjoy.utils.SideBar;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by dllo on 15/10/30.
 */
public class NativeFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private ListView nativeListView;
    private FindSongs finder;
    private List<Mp3Info> mp3Infos;
    private MyListViewAdapter adapter;
    private ImageView nativeback;
    private SideBar sideBar;
    private TextView dialog;
    private List<PlayMusicEntity> data;
    private PlayMusicEntity entity;
    private List<MusicInfoBean> musicInfoBeans;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_native, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        data = new ArrayList<>();
        nativeback = (ImageView) getActivity().findViewById(R.id.nativeback);
        nativeback.setOnClickListener(this);
        initView();
        finder = new FindSongs();
        mp3Infos = finder.getMp3Infos(getActivity().getContentResolver());
        finder.setListAdpter(getActivity().getApplicationContext(), mp3Infos, nativeListView);
        adapter = new MyListViewAdapter(getActivity().getApplicationContext(), mp3Infos);
        // 将得到的数据添加到行布局的实体类中
        nativeListView.setAdapter(adapter);
        SharedPreferences preferences = getActivity().getSharedPreferences("xiaoxin", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("count", mp3Infos.size());
        editor.commit();
        editor.clear();

    }

    //初始化
    private void initView() {
        //显示字母的控件
        sideBar = (SideBar) getActivity().findViewById(R.id.sidrbar);
        dialog = (TextView) getActivity().findViewById(R.id.dialog);
        sideBar.setTextView(dialog);
        //对字母进行监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                // 该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    nativeListView.setSelection(position);
                }

            }

        });
        //listview初始化和监听
        nativeListView = (ListView) getActivity().findViewById(R.id.nativeListView);
        nativeListView.setOnItemClickListener(this);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 这里要利用adapter.getItem(position)来获取当前position所对应的对象
        Toast.makeText(getActivity(),
                ((Mp3Info) adapter.getItem(position)).getArtist(),
                Toast.LENGTH_SHORT).show();
        for (Mp3Info mp3Info : mp3Infos) {
            entity = new PlayMusicEntity();
            entity.setSingerName(mp3Info.getArtist());
            entity.setSongName(mp3Info.getTitle());
            entity.setPicUrl(mp3Info.getAlbum());
            entity.setUrl(mp3Info.getUrl());
            data.add(entity);
        }
        Log.i("qq", data.get(position).getSingerName());
        EventBus.getDefault().post(new MyEvent.PassData(data, position));
    }

}
